/*
 *  Copyright (c) 1998-2001, The University of Sheffield.
 *
 *  This file is part of GATE (see http://gate.ac.uk/), and is free
 *  software, licenced under the GNU Library General Public License,
 *  Version 2, June 1991 (in the distribution as file licence.html,
 *  and also available at http://gate.ac.uk/gate/licence.html).
 *
 *  Main.java
 *
 *  Valentin Tablan, 27-Apr-2006
 *
 *  $Id$
 */

package gatecourse;

import java.io.File;
import java.net.URL;
import java.util.*;
import gate.*;
import gate.creole.*;
import gate.creole.splitter.SentenceSplitter;
import gate.creole.tokeniser.DefaultTokeniser;
import gate.gui.MainFrame;
import gate.util.persistence.*;

public class Main {
  /**
   * @param args
   */
  public static void main(String[] args) {
    System.out.println("Using GATE as a library!");
    try{
      Gate.setGateHome(new File("Z:\\gate"));
      Gate.setUserConfigFile(new File(
              "Z:\\eclipse-workspace\\gatecourse\\gate.xml"));
      Gate.init();
      showGui();
      
    }catch(Exception e){
      e.printStackTrace();
    }
  }
  
  public static void showGui(){
    MainFrame mainFrame = new MainFrame();
    mainFrame.setSize(800, 600);
    mainFrame.setVisible(true); 
  }

  public static void test2()throws Exception{
    
    //tell GATE about the ANNIE plugin
//    Gate.getCreoleRegister().registerDirectories(
//      new File("Z:\\gate\\plugins\\ANNIE").toURL());
    
    //let's load a document
    Document doc = Factory.newDocument("This is a document");
    //create a corpus
    Corpus corpus = Factory.newCorpus("A Corpus");
    //add the document to the corpus
    corpus.add(doc);
    //create a new document...and add it to the corpus
    doc = Factory.newDocument(new URL("http://gate.ac.uk/teaching.html"));
    corpus.add(doc);
//    test1(corpus);
    //let's load a saved application
    SerialAnalyserController application = (SerialAnalyserController)
            PersistenceManager.loadObjectFromFile(new File(
            "C:\\Documents and Settings\\valyt\\Desktop\\APP-2\\annie.gapp"));
    application.setCorpus(corpus);
    application.execute();
    //let's extract some data from the document
    AnnotationSet aSet = doc.getAnnotations();
    Set annTypes = new HashSet();
    annTypes.add("Location");
    annTypes.add("Person");
    annTypes.add("Organization");
    AnnotationSet filtered = aSet.get(annTypes);
    
    Iterator annIter = filtered.iterator();
    while(annIter.hasNext()){
      Annotation ann = (Annotation)annIter.next();
      System.out.println(ann);
    }
    //create a large annotation spanning all the ones selected
    Long start = filtered.firstNode().getOffset();
    Long end = filtered.lastNode().getOffset();
    FeatureMap features = Factory.newFeatureMap();
    features.put("Createdby", "API");
    aSet.add(start, end, "Interesting", features);
    
    System.out.println(doc.toXml(filtered, true));
    
    System.out.println("Execution finished!");

  }
  public static void test1(Corpus corpus) throws Exception{
    //let's create a Tokeniser
    FeatureMap params = Factory.newFeatureMap();
    FeatureMap features = Factory.newFeatureMap();
    features.put("foo", "bar");
    DefaultTokeniser tok = (DefaultTokeniser)Factory.createResource(
            "gate.creole.tokeniser.DefaultTokeniser", params, 
            features, "A Tokeniser");
    //run the tokeniser
//    tok.setDocument(doc);
//    tok.execute();
    //run the tokeniser over the whole corpus
    //create a controller (a.k.a. GATE application)
    SerialAnalyserController controller = (SerialAnalyserController)Factory.
    createResource("gate.creole.SerialAnalyserController");
    controller.add(tok);
    //create a Sentence Splitter
    SentenceSplitter splitter = (SentenceSplitter)Factory.createResource(
            "gate.creole.splitter.SentenceSplitter");
    controller.add(splitter);
    controller.setCorpus(corpus);
    controller.execute();
  }
}
